import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";

class Header extends StatelessWidget implements PreferredSizeWidget {
  final Widget? title;
  final List<Widget>? actions;

  const Header({super.key, this.title, this.actions});

  @override
  Size get preferredSize => const Size.fromHeight(48);

  @override
  Widget build(BuildContext context) {
    final ModalRoute<dynamic>? parentRoute = ModalRoute.of(context);
    Widget? leading;
    if (parentRoute?.impliesAppBarDismissal ?? false) {
      leading = const IconButton(
        icon: Icon(Icons.chevron_left_rounded, color: Colors.white),
        iconSize: 32,
        onPressed: Stacker.pop,
      );
    }
    double top = MediaQuery.paddingOf(context).top;
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light
          .copyWith(statusBarColor: Colors.transparent),
      child: Container(
        padding: EdgeInsets.only(top: top),
        height: preferredSize.height + top,
        decoration: const BoxDecoration(color: ThemeVars.colorPrimary),
        child: Stack(
          alignment: Alignment.center,
          children: [
            if (leading != null) Positioned(left: 10, child: leading),
            if (title != null)
              DefaultTextStyle(
                style: DefaultTextStyle.of(context).style.copyWith(
                      color: Colors.white,
                      fontSize: 18,
                      fontWeight: FontWeight.w500,
                    ),
                child: title!,
              ),
            if (Utils.isNotEmpty(actions))
              Positioned(right: 10, child: Row(children: actions!)),
          ],
        ),
      ),
    );
  }
}
